/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
package vn.demand.dragon.view
{
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.ui.Keyboard;
	
	import flexed.widgets.alerts.alert;
	
	import mx.collections.ICollectionView;
	import mx.collections.IViewCursor;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridItemRenderer;
	import mx.events.AdvancedDataGridEvent;
	import mx.events.AdvancedDataGridEventReason;
	import mx.events.CloseEvent;
	import mx.events.ValidationResultEvent;
	import mx.validators.Validator;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.MarketingApplicationFacade;
	import vn.demand.dragon.editor.EditorComboBox;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.QuotationProxy;
	import vn.demand.dragon.view.components.QuotationLineForm;
	import vn.demand.dragon.view.components.QuotationPanel;
    
    /**
     * A Mediator for interacting with the EmployeeList component
     */
    public class QuotationLineFormMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "QuotationLineFormMediator";

		private var quotationProxy:QuotationProxy;
		private var masterdataProxy:MasterdataProxy;
		
		private var myGrid:AdvancedDataGrid;
		
		private var curValidator:Validator;
		
		private var m:ContextMenu;
		
		private var quotationVO:Object;
		
		private var closing:Boolean = false;
		    
		public static function makeId(quotationVO:Object):String {
        	return NAME + "::" + quotationVO.quote_code;
        }
		        
        /**
         * Constructor. 
         */
        public function QuotationLineFormMediator(quotationVO:Object, mediatorId:String, viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( mediatorId, viewComponent );
			
			this.quotationVO = quotationVO;
			
			quotationProxy = QuotationProxy( facade.retrieveProxy( mediatorId ) );
			masterdataProxy = MasterdataProxy( facade.retrieveProxy( MasterdataProxy.NAME ) );
			
			
			
			quotationLineForm.addEventListener(QuotationLineForm.SAVE_QITEMS, doSaveQItems);
			quotationLineForm.addEventListener(QuotationLineForm.REMOVE_QITEM, onRemoveQItem);
			quotationLineForm.addEventListener(QuotationLineForm.RELOAD_QITEMS, doReloadQItems);
			quotationLineForm.addEventListener(QuotationLineForm.QUOTATION_LINE_FORM_UI_CREATED, prepareData);
			quotationLineForm.addEventListener(QuotationLineForm.CLOSE_LINES_FORM, onCloseLineForm);
        }
        
        public function onCloseLineForm(event:Event):void {
        	var m:QuotationPanelMediator = facade.retrieveMediator(QuotationPanelMediator.makeId(quotationVO)) as QuotationPanelMediator;
        	sendNotification(MarketingApplicationFacade.CLOSE_QUOTATION_PANEL, m.panel, QuotationPanel(m.panel).viewId);
        }
        
        public function prepareData(event:Event):void {
        	myGrid = quotationLineForm.myGrid;
			
			for each (var col:AdvancedDataGridColumn in myGrid.columns) {
				switch (col.dataField) {
					case 'qitem_item':
					case 'qitem_vat_type':
					case 'qitem_cargo_type':
					case 'qitem_container':
					case 'qitem_method':
					case 'qitem_shipping_type':
					case 'qitem_unit':
						col.labelFunction = comboBoxLabelFunction;
						break;
					case 'line_no':
						col.labelFunction = lfRowNum;
						break;
					case 'qitem_price_apply_mode':
						col.labelFunction = lfPriceApplyMode;
						break;
					case 'qitem_surchange1_apply_mode':
						col.labelFunction = lfSurcharge1PriceApplyMode;
						break;
					case 'qitem_surchange2_apply_mode':
						col.labelFunction = lfSurcharge2PriceApplyMode;
						break;
				}
			}
			
			
			myGrid.addEventListener(AdvancedDataGridEvent.ITEM_EDIT_BEGIN, makeEditor);
			myGrid.addEventListener(AdvancedDataGridEvent.ITEM_EDIT_END, processData);
        	
        	doLoadQuotationDetails(quotationVO);
        	
        	switch (int(quotationVO.quote_status)) {
				case 9:
					quotationLineForm.canEdit = true;
					break;
				case 1:
					quotationLineForm.canEdit = false;
					break;
				case 2:
					quotationLineForm.canEdit = false;
					break;
				default:
					alert.info("quotation status: " + quotationVO.quote_status);
			}
			
			createContextMenu();
        } 
        
        var copyMenu:ContextMenuItem;
        var pasteMenu:ContextMenuItem;
        var removeMenu:ContextMenuItem;
        
        private function createContextMenu():void {
		    m = new ContextMenu();
		    m.hideBuiltInItems();
		    var customItemsArr:Array = new Array();
		    copyMenu = new ContextMenuItem("Copy Row");
		    pasteMenu = new ContextMenuItem("Patse Row");
		    removeMenu = new ContextMenuItem("Remove Row");
		    
		    copyMenu.addEventListener("menuItemSelect", copyHandler);
		    pasteMenu.addEventListener("menuItemSelect", pasteHandler);
		    removeMenu.addEventListener("menuItemSelect", removeHandler);
		    
		    pasteMenu.enabled = quotationLineForm.canEdit && false;
		    removeMenu.enabled = quotationLineForm.canEdit;
		    
		    customItemsArr.push(copyMenu);
		    customItemsArr.push(pasteMenu);
		    customItemsArr.push(removeMenu);
		    
		    m.customItems = customItemsArr;
		    
		    myGrid.contextMenu = m;
	    }
	    
	    private static var copyItem:Object = null;
	    
	    private function copyHandler(e:ContextMenuEvent):void
	    {
	    	var itemIndex:Number = -1;
	    	if (e.mouseTarget is AdvancedDataGridItemRenderer) {
		    	itemIndex = AdvancedDataGridItemRenderer(e.mouseTarget).listData.rowIndex;
	    	} else {
	    		itemIndex = myGrid.selectedIndex;
	    	}
	    	if (itemIndex < 0) return;
	    	pasteMenu.enabled = quotationLineForm.canEdit && true;
	    	sendNotification(MarketingApplicationFacade.QUOTATION_LINE_TO_CLIPBOARD, copyItem, this.mediatorName);
		    copyItem = myGrid.dataProvider.getItemAt(itemIndex);
	    }
	    
	    private function pasteHandler(e:ContextMenuEvent):void
	    {
	    	if (!quotationLineForm.canEdit) {
	    		return;
	    	}
		    var itemIndex:Number = -1;
	    	if (e.mouseTarget is AdvancedDataGridItemRenderer) {
		    	itemIndex = AdvancedDataGridItemRenderer(e.mouseTarget).listData.rowIndex;
	    	} else {
	    		itemIndex = myGrid.selectedIndex;
	    	}
	    	if (itemIndex < 0) return;
	    	quotationProxy.copyQItem(copyItem, itemIndex);
	    	quotationLineForm.enableSave = true;
	    }
	    
	    private function removeHandler(e:ContextMenuEvent):void
	    {
	    	if (!quotationLineForm.canEdit) {
	    		return;
	    	}
	    	var itemIndex:Number = -1;
	    	if (e.mouseTarget is AdvancedDataGridItemRenderer) {
		    	itemIndex = AdvancedDataGridItemRenderer(e.mouseTarget).listData.rowIndex;
	    	} else {
	    		itemIndex = myGrid.selectedIndex;
	    	}
	    	if (itemIndex < 0) {
	    		alert.error("You don't select any row to remove!");
	    		return;
	    	}
		    doRemoveQItem(itemIndex);
	    }
        
        public function doSaveQItems(event:Event):Boolean {
        	if (!doValidate()) {
        		alert.error(myGrid.errorString);
        		return false;
        	}
        	quotationLineForm.enableSave = false;
        	var cursor:IViewCursor = ICollectionView(myGrid.dataProvider).createCursor();
        	while (!cursor.afterLast) {
        		RIALogger.debug(this, "remove?: qitem_item: " + Number(cursor.current['qitem_item']));
        		if ((Number(cursor.current['qitem_item']) <= 0)) {
        			cursor.remove();
        		} else {
        			cursor.moveNext();
        		}
        	} 
        	
        	sendNotification(MarketingApplicationFacade.SAVE_QITEMS, null, this.mediatorName);
        	
        	quotationProxy.saveQuotationLines();
        	return true;
        }
        
        public function doValidate():Boolean {
        	var errors:String = "";
        	var i:int = 1;
        	for each (var item:Object in myGrid.dataProvider) {
        		if (Number(item.qitem_item) == 0) {
        			//errors += 'Row [' + i + ']: Service can not be empty\n';
        			continue;
        		}
        		if (Number(item.qitem_cargo_type) == 0) {
        			errors += 'Row [' + i + ']: Cargo Type can not be empty\n';
        		}
        		if (Number(item.qitem_shipping_type) == 0) {
        			errors += 'Row [' + i + ']: Shipping Type can not be empty\n';
        		}
        		i++;
        		if (i == myGrid.dataProvider.length) {
        			break;
        		}
        	}
        	RIALogger.debug(this, 'errors message after validate: \n' + errors); 
        	if (errors.length > 0) {
        		myGrid.errorString = errors;
        		//quotationLineForm.enableSave = false;
        		return false;
        	}
    		//quotationLineForm.enableSave = true;
    		myGrid.errorString = "";
    		return true;
        }
        
        public function onRemoveQItem(event:Event):void {
        	var selIndex:int = myGrid.selectedIndex;
        	if (selIndex < 0) {
        		alert.error("You don't select any row to remove!");
        		return;
        	}
        	doRemoveQItem(selIndex);
        }
        
        public function doRemoveQItem(selIndex:int):void {
        	alert.confirm("Do you want to remove row at [" + (selIndex + 1) + "]?", function(e:CloseEvent) {
        		if (e.detail != Alert.YES) {
        			return;
        		}
        		quotationProxy.removeQItem(selIndex);
        		doValidate();
        		myGrid.selectedIndex = -1;
        		this.quotationLineForm.enableSave = true;
        	});
        }
        
        public function doReloadQItems(event:Event):void {
        	if (quotationLineForm.enableSave) {
	        	alert.confirm("Data has being edited, do you want to reload?", function(e:CloseEvent) {
	        		if (e.detail == Alert.YES) {
	        			doLoadQuotationDetails(quotationVO);
	        		}
	        	});
        	} else {
        		doLoadQuotationDetails(quotationVO);
        	}
        }

        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [ 
            		MarketingApplicationFacade.LOAD_QUOTATION_DETAIL_SUCCESS,
            		MarketingApplicationFacade.LOAD_QUOTATION_DETAIL_FAILED,
            		MarketingApplicationFacade.QITEMS_UPDATED,
            		MarketingApplicationFacade.QITEMS_UPDATE_FAILED,
            		MarketingApplicationFacade.QUOTATION_LINE_TO_CLIPBOARD,
            		MarketingApplicationFacade.QUOTATION_ACTIVATED,
	            	MarketingApplicationFacade.QUOTATION_UN_ACTIVATED,
	            	MarketingApplicationFacade.QUOTATION_COMPLETED,
					 ];
        }
        		
        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
        	if (note.getType() != this.getMediatorName() && note.getType() != QuotationFormMediator.makeId(quotationVO)) {
        		return;
        	}
            switch ( note.getName() ) 
			{
            	case MarketingApplicationFacade.LOAD_QUOTATION_DETAIL_SUCCESS:
            		var emptyItem:Object = quotationProxy.newQuotationLine()
            		myGrid.dataProvider = quotationProxy.quotationLinesDP;
            		myGrid.errorString = null;
            		quotationLineForm.enableSave = false;
					myGrid.dataProvider.addItem(emptyItem);
            		break;
            	
            	case MarketingApplicationFacade.LOAD_QUOTATION_DETAIL_FAILED:
            		quotationLineForm.enableSave = false;
            		myGrid.errorString = quotationProxy.errorStatus;
            		break;
            	
            	case MarketingApplicationFacade.QITEMS_UPDATED:
            		myGrid.dataProvider.addItem(quotationProxy.newQuotationLine());
            		alert.info("Save quotation lines finished!");
            		break;
            	
            	case MarketingApplicationFacade.QITEMS_UPDATE_FAILED:
            		myGrid.dataProvider.addItem(quotationProxy.newQuotationLine());
            		quotationLineForm.enableSave = true;
            		alert.error("Save quotation lines failed: \n" + note.getBody());
            		break;
            	case MarketingApplicationFacade.QUOTATION_LINE_TO_CLIPBOARD:
            		pasteMenu.enabled = true;
            		break;
            	case MarketingApplicationFacade.QUOTATION_ACTIVATED:
					quotationLineForm.canEdit = false;
					quotationLineForm.enableSave = false;
					pasteMenu.enabled = false;
					removeMenu.enabled = false;
					break;
            	case MarketingApplicationFacade.QUOTATION_UN_ACTIVATED:
            		quotationLineForm.canEdit = true;
            		quotationLineForm.enableSave = true;
					pasteMenu.enabled = true;
					removeMenu.enabled = true;
            		break;
            	case MarketingApplicationFacade.QUOTATION_COMPLETED:
            		quotationLineForm.canEdit = false;
            		quotationLineForm.enableSave = false;
					pasteMenu.enabled = false;
					removeMenu.enabled = false;
            		break;
            }
        }
        
        public function doLoadQuotationDetails(quotationVO:Object) {
        	quotationProxy.loadQuotationDetails(quotationVO.quote_id);
        }
                    
        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return EmployeeList the viewComponent cast to EmployeeList
         */
        public function get quotationLineForm():QuotationLineForm
		{
            return viewComponent as QuotationLineForm;
        }
        
		public function makeEditor(event:AdvancedDataGridEvent):void {				
			var columnName:String = myGrid.columns[event.columnIndex].dataField;
			var editable:Boolean = Boolean(myGrid.columns[event.columnIndex].editable);
			var cbDataProvider:Object = null;
			
			var sel:Object = myGrid.dataProvider.getItemAt(myGrid.selectedIndex);
				
		 	switch (columnName) {
		 		case 'qitem_price_apply_mode':
		 		case 'qitem_surchange1_apply_mode':
		 		case 'qitem_surchange2_apply_mode':
		 			event.preventDefault();
	                myGrid.createItemEditor(event.columnIndex, event.rowIndex);
	                var value = myGrid.editedItemRenderer.data[columnName];
	                CheckBox(myGrid.itemEditorInstance).selected = value == true || value > 0;
	                CheckBox(myGrid.itemEditorInstance).addEventListener(KeyboardEvent.KEY_DOWN, handleKeyArrow);
	                CheckBox(myGrid.itemEditorInstance).setFocus();
	                return;
		 	}
			if (columnName == 'qitem_vat_type') {
				if (Number(sel.qitem_item) == NaN || Number(sel.qitem_item) < 1 || !masterdataProxy.isItemVAT(sel.qitem_item)) {
					event.preventDefault();
					return;
				}  
				cbDataProvider = masterdataProxy.vatTypesDP;
			} else if (columnName == 'qitem_cargo_type') {
                cbDataProvider = masterdataProxy.cargoTypesDP;                
            } else if (columnName == 'qitem_container') {
                cbDataProvider = masterdataProxy.containersDP;
            } else if (columnName == 'qitem_method') {
                cbDataProvider = masterdataProxy.methodsDP
            } else if (columnName == 'qitem_shipping_type') {
                cbDataProvider = masterdataProxy.shippingTypesDP;
            } else if (columnName == 'qitem_item') {
                cbDataProvider = masterdataProxy.taskItemsDP;
            }
            else if (columnName == 'qitem_unit') {
                cbDataProvider = masterdataProxy.unitsDP;
            } 
                
            if (null != cbDataProvider){
            	// Disable copying data back to the control.
                event.preventDefault();
                myGrid.createItemEditor(event.columnIndex, event.rowIndex);
                var cb:EditorComboBox = EditorComboBox(myGrid.itemEditorInstance);
                cb.dataProvider = cbDataProvider;
                cb.selectedData = myGrid.editedItemRenderer.data[columnName];
                cb.setFocus();
            }
            else {
            	event.preventDefault();
                myGrid.createItemEditor(event.columnIndex, event.rowIndex);
                TextInput(myGrid.itemEditorInstance).text = String(myGrid.editedItemRenderer.data[columnName]);
                TextInput(myGrid.itemEditorInstance).addEventListener(KeyboardEvent.KEY_DOWN, handleKeyArrow);
                TextInput(myGrid.itemEditorInstance).setFocus();
            }
            //[{rowIndex: selectedRowIndex, columnIndex:selectedColIndex}]
            setupValidator(columnName, myGrid.itemEditorInstance);
            myGrid.selectedCells = [{'rowIndex': event.rowIndex, 'columnIndex': event.columnIndex}]; 
        }
        
        function setupValidator(columnName:String, itemEditor:Object):void {
        	var sel:Object = myGrid.dataProvider.getItemAt(myGrid.selectedIndex);
        	if (columnName == 'qitem_quantity_from') {
	        	RIALogger.debug(this, "setupValidator for: " + columnName);
        		var ti:TextInput = null;
        		quotationLineForm.quantityValidator.source = itemEditor;
        		quotationLineForm.quantityValidator.property = 'text';
        		quotationLineForm.quantityValidator.required = true;
	        	quotationLineForm.quantityValidator.minValue = NaN;
        		if (Number(sel.qitem_quantity_to) > 0) {
	        		quotationLineForm.quantityValidator.maxValue = Number(sel.qitem_quantity_to);
        		} else {
	        		quotationLineForm.quantityValidator.maxValue = NaN;
        		}
        		curValidator = quotationLineForm.quantityValidator;
        	}
        	else if (columnName == 'qitem_quantity_to') {
        		quotationLineForm.quantityValidator.source = itemEditor;
        		quotationLineForm.quantityValidator.property = 'text';
        		quotationLineForm.quantityValidator.required = false;
	        	quotationLineForm.quantityValidator.maxValue = NaN;
        		if (Number(sel.qitem_quantity_from) > 0) {
	        		quotationLineForm.quantityValidator.minValue = Number(sel.qitem_quantity_from);
        		} else {
	        		quotationLineForm.quantityValidator.minValue = NaN;
        		}
        		curValidator = quotationLineForm.quantityValidator;
        	} 
        	else if ('qitem_price' == columnName) {
        		quotationLineForm.priceValidator.source = itemEditor;
        		quotationLineForm.priceValidator.property = 'text';
        		quotationLineForm.priceValidator.required = true;
	        	quotationLineForm.priceValidator.maxValue = NaN;
	        	quotationLineForm.priceValidator.minValue = NaN;
        		curValidator = quotationLineForm.priceValidator;
        	}
        	else if (['qitem_surchange1_price', 'qitem_surchange2_price'].indexOf(columnName) >= 0) {
        		quotationLineForm.priceValidator.source = itemEditor;
        		quotationLineForm.priceValidator.property = 'text';
        		quotationLineForm.priceValidator.required = false;
	        	quotationLineForm.priceValidator.maxValue = NaN;
	        	quotationLineForm.priceValidator.minValue = NaN;
        		curValidator = quotationLineForm.priceValidator;
        	} 
        	else {
	        	curValidator = null;
        	}
        }
            
        public function handleKeyArrow(event:KeyboardEvent):void {
        	if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP) {
        		event.stopPropagation();
            	var newEvent:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN, false, false, Keyboard.ENTER, Keyboard.ENTER, 13, event.ctrlKey, event.altKey, event.keyCode == Keyboard.UP);
            	var b:Boolean = event.currentTarget.dispatchEvent(newEvent);
        	}            	
        }
        
        //handle datagrid cell edit end event
        public function processData(event:AdvancedDataGridEvent):void {
        	if (event.reason == AdvancedDataGridEventReason.CANCELLED) {
        		return;
        	}        	
        	//validate data
        	var colName:String = event.dataField;
        	if (curValidator != null) {
	    		var vResult:ValidationResultEvent = curValidator.validate();
	    		if (vResult.type == ValidationResultEvent.INVALID) {
	    			event.preventDefault();
	    			return;
	    		}
        	}
        	if (event.rowIndex == (myGrid.dataProvider.length - 1)) {
        		var emptyItem:Object = quotationProxy.newQuotationLine()
				myGrid.dataProvider.addItem(emptyItem);
				sendNotification(MarketingApplicationFacade.ADD_EMPTY_QITEM, emptyItem); 
			}
			quotationLineForm.enableSave = true;
//			if (event.dataField == 'qitem_price_apply_mode') {
//				var myEditor:CheckBox = CheckBox(event.currentTarget.itemEditorInstance);
//				if (myEditor.selected) {
//					var item_id:int = event.itemRenderer.data['qitem_item'];
//					var qitem_id:int = event.itemRenderer.data['qitem_id'];
//					var list:Object = quotationProxy.getSameServiceLines(item_id, qitem_id);
//					for each(var xml:Object in list) {
//						xml.qitem_price_apply_mode = false;
//					}
//				}
//			}
        }
        
        public function comboBoxLabelFunction(item:Object, column:AdvancedDataGridColumn):String {
        	switch (column.dataField) {
        		case 'qitem_item': 
        			return masterdataProxy.getItemById(item[column.dataField]) as String;
        		case 'qitem_vat_type': 
        			return masterdataProxy.getVATTypeById(item[column.dataField]);
        		case 'qitem_cargo_type':
        			return masterdataProxy.getCargoTypeById(item[column.dataField]);
        		case 'qitem_method':
        			return masterdataProxy.getMethodById(item[column.dataField]);
        		case 'qitem_container':
        			return masterdataProxy.getContainerById(item[column.dataField]);
        		case 'qitem_shipping_type':
        			return masterdataProxy.getShippingTypeById(item[column.dataField]);
        		case 'qitem_unit':
        			return masterdataProxy.getUnitById(item[column.dataField]);
        		default:
        			return '';
        	}
        }        
        
        public function lfPriceApplyMode(item:Object, column:AdvancedDataGridColumn):String {
        	return item.qitem_price_apply_mode == true || item.qitem_price_apply_mode > 0 ? "X" : "";
        }
        
        public function lfSurcharge1PriceApplyMode(item:Object, column:AdvancedDataGridColumn):String {
        	return item.qitem_surchange1_apply_mode == true || item.qitem_surchange1_apply_mode > 0 ? "X" : "";
        }
        
        public function lfSurcharge2PriceApplyMode(item:Object, column:AdvancedDataGridColumn):String {
        	return item.qitem_surchange2_apply_mode == true || item.qitem_surchange2_apply_mode > 0 ? "X" : "";
        }
        
        public function lfRowNum(item:Object, col:int):String {
        	if (Number(item.qitem_item) > 0) {
		        return String(myGrid.dataProvider.getItemIndex(item) + 1);
        	}
        	return "";
        }    
        
    }
}